package cn.droidlover.xdroidmvp.kit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * String相关的工具类
 *
 * @author changxiaofei
 * @time 2013-3-25 下午1:37:45
 */
public class StringUtil {
    private static final String TAG = "StringUtil";

    /**
     * 参数不为null且不为""，返回true；否则返回false
     *
     * @param strParam
     * @return
     */
    public static boolean isNotEmpty(String strParam) {
        if (strParam != null && !"".equals(strParam.trim()) && !"null".equals(strParam.trim().toLowerCase())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 将字符串日期转换成长整类型日期
     *
     * @param strDate 字符串型时间
     * @return long 长整型时间
     */
    public static String formatDateStringToInt(String strDate) {
        SimpleDateFormat format;
        Date time;
        if (strDate.trim().equals("")) {
            return null;
        }
        String strAry[] = strDate.split(":");
        if (strAry.length > 1) {
            format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale
                    .getDefault());
        } else {
            format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        }
        try {
            time = format.parse(strDate + ":00");
            return time.getTime() / 1000 + "";
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 参数为null、 "null"或" "，返回true；否则返回false
     *
     * @param strParam
     * @return
     */
    public static boolean isEmpty(String strParam) {
        if (strParam == null || "".equals(strParam.trim()) || "null".equals(strParam.trim().toLowerCase())) {
            return true;
        } else {
            return false;
        }
    }

    public static String longtoString(String date) {
        String lTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        Date dt2;
        try {
            dt2 = sdf.parse(date);
            lTime = dt2.getTime() / 1000 + "";
            return lTime;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return lTime;

    }

    /**
     * 格式化时间
     *
     * @return
     */
    public static String dateDiff(String startTime, String endTime) {
        long diff;
        diff = Math.abs((Long.parseLong(endTime) * 1000 - Long.parseLong(startTime) * 1000));
        if (diff / (1000 * 60 * 60) == 0) {
            return diff / (1000 * 60) + "min";
        } else {
            return diff / (1000 * 60 * 60) + "h";
        }
    }


    public static String StringToLongToString(String searcherInput) {
        if (StringUtil.isEmpty(searcherInput) || searcherInput.equals("0")) {
            return "";
        }
        long lgTime = 0;
        Boolean judge = false;// 代表不能转成long
        try {
            Integer.parseInt(searcherInput);
            searcherInput += "000";
            judge = true;
        } catch (NumberFormatException e) {
            judge = false;
        }
        if (judge == true) {
            lgTime = Long.parseLong(searcherInput);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
            searcherInput = sdf.format(lgTime);
        }
        return searcherInput;

    }

    public static String stringtodate(String searcherInput) {
        if (StringUtil.isEmpty(searcherInput) || searcherInput.equals("0")) {
            return "";
        }
        long lgTime = 0;
        Boolean judge = false;// 代表不能转成long
        try {
            Integer.parseInt(searcherInput);
            searcherInput += "000";
            judge = true;
        } catch (NumberFormatException e) {
            judge = false;
        }
        if (judge == true) {
            lgTime = Long.parseLong(searcherInput);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            searcherInput = sdf.format(lgTime);
        }
        return searcherInput;

    }

    /**
     * 获取当前时间
     */
    public static String getNowDate() {
        String temp_str = "";
        Date dt = new Date();
        //最后的aa表示“上午”或“下午”    HH表示24小时制    如果换成hh表示12小时制
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        temp_str = sdf.format(dt);
        return temp_str;
    }

    /**
     * 比较日期
     */
    public static int compare_date(String DATE1, String DATE2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");//超时
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
}
